package com.wzx.user.sdk.utils;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * @author wzx
 * @version 1.0
 * @date 2023/3/16 - 11:24
 * @description 标题
 */
@Slf4j
@Service
public class SmsUtils {

    @Value("${demoservice.sms.validateCodeTemplateCode}")
    private String CODE;

    @Value("${demoservice.sms.signName}")
    private String SIGN_NAME;

    @Value("${demoservice.sms.accessKeyId}")
    private String ACCESS_KEY_ID;

    @Value("${demoservice.sms.accessKeySecret}")
    private String ACCESS_KEY_SECRET;

    //发送短信
    public boolean sendMessage(String phoneNumbers,String code) throws ClientException {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou",
                ACCESS_KEY_ID,
                ACCESS_KEY_SECRET);
        IAcsClient client = new DefaultAcsClient(profile);

        SendSmsRequest request = new SendSmsRequest();
        request.setSysRegionId("cn-hangzhou");
        request.setPhoneNumbers(phoneNumbers);
        request.setSignName(SIGN_NAME);
        request.setTemplateCode(CODE);
        request.setTemplateParam("{\"code\":\"" + code + "\"}");
        try {
            SendSmsResponse response = client.getAcsResponse(request);
            log.info("手机号：{}，发送短信：{}",phoneNumbers,response.getMessage());
            return response.getCode().equals("OK");

        } catch (ClientException e) {
            log.error("发送错误!",e);
            throw e;
        }
    }

}
